package com.example.workflow.controller;

import com.example.workflow.entity.SysProcess;
import com.example.workflow.service.SysProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.example.workflow.common.Result;
import com.example.workflow.vo.ProcessDetailVO;

import java.util.List;

@RestController
@RequestMapping("/api/process")
@RequiredArgsConstructor
public class SysProcessController {
    private final SysProcessService processService;
    
    @GetMapping("/list/{systemId}")
    public Result<List<SysProcess>> listBySystemId(@PathVariable Long systemId) {
        return Result.success(processService.listBySystemId(systemId));
    }
    
    @PostMapping("/add")
    public Result<Void> addProcess(@RequestBody SysProcess process) {
        processService.addProcess(process);
        return Result.success();
    }
    
    @PostMapping("/update/{id}")
    public Result<SysProcess> updateProcess(
            @PathVariable("id") Long id,
            @RequestParam("name") String name,
            @RequestParam("key") String key,
            @RequestParam("file") MultipartFile file) {
        try {
            SysProcess process = processService.updateProcess(id, name, key, file);
            return Result.success(process);
        } catch (Exception e) {
            return Result.error("更新流程失败：" + e.getMessage());
        }
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteProcess(@PathVariable Long id) {
        processService.deleteProcess(id);
        return Result.success();
    }
    
    @PostMapping("/deploy")
    public Result<SysProcess> deployProcess(
            @RequestParam("systemId") Long systemId,
            @RequestParam("name") String name,
            @RequestParam("key") String key,
            @RequestParam("file") MultipartFile file) {
        try {
            SysProcess process = processService.deployProcess(systemId, name, key, file);
            return Result.success(process);
        } catch (Exception e) {
            return Result.error("部署流程失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/xml/{id}")
    public Result<ProcessDetailVO> getProcessDetail(@PathVariable Long id) throws Exception{
            ProcessDetailVO detail = processService.getProcessDetail(id);
            return Result.success(detail);
    }
} 